package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevChangeRecordDao;
import com.sugon.dao.DeviceInfoDao;
import com.sugon.entity.DevChangeRecordEntity;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.service.DevChangeRecordService;
import com.sugon.service.DeviceInfoService;
import com.sugon.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 设备变更记录Service实现类
 *
 * @author sugon
 * @date 2019-07-12 08:49:29
 */
@Service("devChangeRecordService")
public class DevChangeRecordServiceImpl implements DevChangeRecordService {

    @Autowired
    private DevChangeRecordDao devChangeRecordDao;
    @Autowired
    private DeviceInfoDao deviceInfoDao;
    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevChangeRecordEntity queryObject(String id) {
        return devChangeRecordDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevChangeRecordEntity> queryList(Map<String, Object> map) {
        return devChangeRecordDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devChangeRecordDao.queryTotal(map);
    }

    /**
     * save
     * save
     *
     * @param devChangeRecord 实体
     * @return
     */
    @Override
    public int save(DevChangeRecordEntity devChangeRecord) {
        devChangeRecord.setId(RandomUtil.randomString(32));
        devChangeRecord.setIsDel(Integer.valueOf(Constant.IS_NOT_DEL));
        devChangeRecord.setCreateDate(DateUtil.date());
        devChangeRecord.setCreateUser(getUserId());
        return devChangeRecordDao.save(devChangeRecord);
    }

    /**
     * update
     *
     * @param devChangeRecord 实体
     * @return
     */
    @Override
    public int update(DevChangeRecordEntity devChangeRecord) {
        devChangeRecord.setUpdateDate(new Date());
        devChangeRecord.setUpdateUser(getUserId());
        return devChangeRecordDao.update(devChangeRecord);
    }

    /**
     * 发哈开发
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devChangeRecordDao.delete(id);
    }

    /**
     * 发哈开发
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devChangeRecordDao.deleteBatch(ids);
    }

    /**
     * getInfoByDevCode
     *
     * @param devCode
     * @return
     */
    @Override
    public DevChangeRecordEntity getInfoByDevCode(String devCode) {
        return devChangeRecordDao.getInfoByDevCode(devCode);
    }

    /**
     * 发哈开发
     *
     * @param ids
     */
    @Override
    public void deleteRecord(String ids) {
        DevChangeRecordEntity devChangeRecord = devChangeRecordDao.queryObject(ids);
    }

    /**
     * 发哈开发
     *
     * @param devChangeRecord
     * @return
     */
    @Override
    public int updateDevChangeRecord(DevChangeRecordEntity devChangeRecord) {
        return devChangeRecordDao.updateDevChangeRecord(devChangeRecord);
    }

    /**
     * 发哈开发
     *
     * @param devChangeRecord
     * @return
     */
    @Override
    public int review(DevChangeRecordEntity devChangeRecord) {
        return devChangeRecordDao.update(devChangeRecord);
    }

    /**
     * 发哈开发
     *
     * @param devChangeRecord
     * @return
     */
    @Override
    public int infoChange(DevChangeRecordEntity devChangeRecord) {
        //修改原数据设备状态
        String devCode = devChangeRecord.getDevCode();
        Integer status = devChangeRecord.getDevStatus();
        deviceInfoDao.updateStatusByCode(status, devCode);
        //添加变更(台账)记录
        DeviceInfoEntity deviceInfoEntity = deviceInfoService.getInfoByDevCode(devCode);
        //台账记录实体(变更)
        devChangeRecord.setUseDept(deviceInfoEntity.getDevFarmName());
        devChangeRecord.setWorkshop(deviceInfoEntity.getDevFarm());//车间名称
        devChangeRecord.setDevNum(1);
        devChangeRecord.setDevModel(deviceInfoEntity.getReserve1());//型号
        devChangeRecord.setSubFactory(deviceInfoEntity.getDevFactories());//工厂
        devChangeRecord.setWorkshop(deviceInfoEntity.getDevFarm());
//        devChangeRecord.setOrigVal(deviceInfoEntity.getOriginVal());//原值
        return save(devChangeRecord);
    }

}
